package com.UnionPayCenter.service.serviceImpl;


import com.UnionPayCenter.entity.ManagerEntity;
import com.UnionPayCenter.entity.UserEntity;
import com.UnionPayCenter.entity.UserLevelEntity;
import com.UnionPayCenter.enums.ErrorCodeEnum;
import com.UnionPayCenter.exception.ApiException;
import com.UnionPayCenter.mapper.ManagerMapper;
import com.UnionPayCenter.mapper.UserLevelMapper;
import com.UnionPayCenter.mapper.UserMapper;
import com.UnionPayCenter.service.IUserLevelService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description:
 * @Param:
 * @return:
 * @Author: bite
 * @Date: 2019/4/25
 */
@Service
@Transactional
public class UserLevelServiceImpl implements IUserLevelService {
    @Autowired
    private UserLevelMapper userLevelMapper;

    @Autowired
    private ManagerMapper managerMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo<UserLevelEntity> findList(JSONObject jsonParam) {
        Integer managerId = jsonParam.getInteger("managerId");
        Integer shopId = jsonParam.getInteger("shopId");
        ManagerEntity managerEntity = managerMapper.selectByShopId(managerId, shopId);
        if (managerEntity != null) {
            //页数
            Integer page = jsonParam.getInteger("page");
            if (page == null) {
                page = 1;
            }
            //每页条数
            Integer pageSize = jsonParam.getInteger("pageSize");
            if (pageSize == null) {
                pageSize = 20;
            }
            PageHelper.startPage(page, pageSize);

            List<UserLevelEntity> list = userLevelMapper.selectList(shopId);
            PageInfo<UserLevelEntity> pageList = new PageInfo<>(list);
            return pageList;
        } else {
            throw new ApiException(ErrorCodeEnum.SW10.code(), String.format(ErrorCodeEnum.SW10.msg(), "managerId"));
        }
    }

    @Override
    public boolean add(JSONObject jsonParam) {
        Integer managerId = jsonParam.getInteger("managerId");
        Integer shopId = jsonParam.getInteger("shopId");
        ManagerEntity managerEntity = managerMapper.selectByShopId(managerId, shopId);
        if (managerEntity != null) {

            String name = jsonParam.getString("name");
            Integer minNumber = jsonParam.getInteger("minNumber");
            Integer maxNumber = jsonParam.getInteger("maxNumber");
            Integer sort = jsonParam.getInteger("sort");
            UserLevelEntity userLevelEntity = new UserLevelEntity();
            userLevelEntity.setName(name);
            userLevelEntity.setMinNumber(minNumber);
            userLevelEntity.setMaxNumber(maxNumber);
            userLevelEntity.setSort(sort);
            userLevelEntity.setShopId(shopId);
            userLevelMapper.insertSelective(userLevelEntity);
            return true;
        } else {
            throw new ApiException(ErrorCodeEnum.SW10.code(), String.format(ErrorCodeEnum.SW10.msg(), "managerId"));
        }
    }

    @Override
    public boolean update(JSONObject jsonParam) {
        Integer managerId = jsonParam.getInteger("managerId");
        Integer shopId = jsonParam.getInteger("shopId");
        ManagerEntity managerEntity = managerMapper.selectByShopId(managerId, shopId);
        if (managerEntity != null) {
            Integer levelId = jsonParam.getInteger("levelId");
            String name = jsonParam.getString("name");
            Integer condition = jsonParam.getInteger("condition");
            Double discount = jsonParam.getDouble("discount");

            List<UserLevelEntity> exists = userLevelMapper.seletByName(shopId, name);
            if (exists != null && exists.size() > 0) {
                for (UserLevelEntity userLevel : exists) {
                    if (userLevel.getLevelId() != levelId) {
                        throw new ApiException(ErrorCodeEnum.FL500.code(), String.format(ErrorCodeEnum.FL500.msg().concat("[%s]"),
                                "等级名称已存在"));
                    }
                }
            }

            UserLevelEntity userLevelEntity = new UserLevelEntity();
            userLevelEntity.setLevelId(levelId);
            userLevelEntity.setName(name);
            userLevelEntity.setLevelCondition(condition);
            userLevelEntity.setDiscount(discount == null ? 10 : discount);
            userLevelMapper.updateByPrimaryKeySelective(userLevelEntity);
            return true;
        } else {
            throw new ApiException(ErrorCodeEnum.SW10.code(), String.format(ErrorCodeEnum.SW10.msg(), "managerId"));
        }
    }

    @Override
    public boolean updateByRecharge(Integer userId, Double amount) {
        UserEntity userEntity = userMapper.selectByPrimaryKey(userId);
        if (userEntity != null) {
            Integer shopId = userEntity.getShopId();
            //根据会员之前等级与充值金额查询充值后应该修改的会员等级
            UserLevelEntity userLevelEntity = userLevelMapper.selectPreLevel(shopId, userEntity.getLevelId(), amount);
            if (userLevelEntity.getLevelId() != userEntity.getLevelId()) {
                userEntity.setLevelId(userLevelEntity.getLevelId());
                userMapper.updateByPrimaryKeySelective(userEntity);
                return true;
            }
        }
        return false;
    }

    /**
     * 获取优惠券信息
     *
     * @param shopId
     * @param levelId
     * @return
     */
    @Override
    public UserLevelEntity selectByLevelId(Integer shopId, Integer levelId) {
        List<UserLevelEntity> list = userLevelMapper.selectList(shopId);
        for (UserLevelEntity entity : list) {
            if (levelId.equals(entity.getLevelId())) {
                return entity;
            }
        }
        return null;
    }

    /**
     *
     * 前端获取等级信息列表
     * @param jsonParam
     * @return
     */
    @Override
    public List<UserLevelEntity> findLevelList(JSONObject jsonParam) {
        Integer shopId = jsonParam.getInteger("shopId");
        List<UserLevelEntity> levelList = userLevelMapper.selectList(shopId);
        return levelList;
    }
}
